package action;

import dao.CartItemDao;
import po.CartItem;
import po.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/user/action/DeleteCartItemServlet")
public class DeleteCartItemServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String userId = ((User)req.getSession().getAttribute("user")).getPhone();
            Integer rid = Integer.parseInt(req.getParameter("rid"));
            Integer row = null;
            CartItemDao cartItemDao = new CartItemDao();
            row = cartItemDao.delete(userId,rid);
            if(row<=0){
                resp.getWriter().print("<script>alert('删除失败！请联系管理员');window.location='login.jsp'</script>");
            }
            //跳转到购物车
            resp.sendRedirect("../action/ShowCartServlet");
    }
}
